# - Try to find MathLink, a C interface to calling Mathematica
# Once done, this will define
#  MathLink_FOUND
#  MathLink_INCLUDE_DIRS
#  MathLink_LIBRARIES

message(STATUS "")
message(STATUS "finding MathLink library")

# get the path to MathLink
if(os STREQUAL "Linux")
    set(math_path /opt/Mathematica/7.0.1)
    if(EXISTS ${math_path})
        set(MathLink_PATH
            ${math_path}/SystemFiles/Links/MathLink/DeveloperKit/Linux-x86-64/CompilerAdditions
        )
    else(EXISTS ${math_path})
        set(MathLink_FOUND false)
        if(MathLink_FIND_REQUIRED)
            message(FATAL_ERROR "MathLink is required, but not found")
        endif(MathLink_FIND_REQUIRED)
    endif(EXISTS ${math_path})
elseif(os STREQUAL "Mac OS X")
    set(math_path /Applications/Mathematica.app)
    if(EXISTS ${math_path})
        set(MathLink_PATH
            ${math_path}/SystemFiles/Links/MathLink/DeveloperKit/CompilerAdditions
        )
    else(EXISTS ${math_path})
        set(MathLink_FOUND false)
        if(MathLink_FIND_REQUIRED)
            message(FATAL_ERROR "MathLink is required, but not found")
        endif(MathLink_FIND_REQUIRED)
    endif(EXISTS ${math_path})
endif(os STREQUAL "Linux")

set(MathLink_INCLUDE_DIRS
        ${MathLink_PATH}
)
if(mach STREQUAL "32")
    if(os STREQUAL "Linux")
        set(MathLink_LIBRARIES "-L${MathLink_PATH} -lML32i3 -lrt")
    elseif(os STREQUAL "Mac OS X")
        set(MathLink_LIBRARIES "-L${MathLink_PATH} -lMLi3 -lstdc++")
    endif(os STREQUAL "Linux")
elseif(mach STREQUAL "64")
    if(os STREQUAL "Linux")
        set(MathLink_LIBRARIES "-L${MathLink_PATH} -lML64i3 -lrt")
    elseif(os STREQUAL "Mac OS X")
        message(FATAL_ERROR "I do not know how to set MathLink_LIBRARIES under 64-bit")
    endif(os STREQUAL "Linux")
endif(mach STREQUAL "32")

message(STATUS "--> found MathLink under ${MathLink_INCLUDE_DIRS}")

set(MathLink_FOUND true)
